<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use SmsManager;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $validator = Validator::make($data, [
            'name' => 'required|max:255',
            'luotest_response'=>'luotest_response',
            //'email' => 'required|email|max:255|unique:users',
			'mobile'=> 'required|zh_mobile|confirm_mobile_not_change|confirm_rule:mobile_required|unique:users,mobile',
			'verifyCode'=>'required|verify_code',
            
			'password' => 'required|min:6|confirmed',
            
		],['zh_mobile'=>'手机格式不正确。','verify_code'=>'验证码错误。','confirm_mobile_not_change'=>'确认验证码对应的手机。','luotest_response'=>'人机验证失败']);

		if($validator->fails()){
			//验证失败后清空存储的发送状态，防止用户重复试错
			//SmsManager::forgetState();
		}
        //var_dump($validator->errors());
		return $validator;
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'mobile' => $data['mobile'],
            'password' => bcrypt($data['password']),
        ]);
    }
}
